package wec_wallet.ebo.com.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.ihongqiqu.util.RegUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import bassproject.ebo.com.ebobass.activity.MyBassAppCompatActivity;
import bassproject.ebo.com.ebobass.http_utils.interfaces.IHttpPostListener;
import bassproject.ebo.com.ebobass.http_utils.utils.Ipost;
import bassproject.ebo.com.ebobass.utils.IToast;
import bassproject.ebo.com.ebobass.utils.I_Share;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wec_wallet.ebo.com.R;
import wec_wallet.ebo.com.utils.Functions;
import wec_wallet.ebo.com.utils.URLs;

import static wec_wallet.ebo.com.activity.main.MineFragment.CODE_REFRESH;


/**
 * Created by admin on 2018/3/29.
 */

public class MailVerificationActivity extends MyBassAppCompatActivity {


    @BindView(R.id.et_mail)
    EditText etMail;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn_code)
    Button btnCode;


    private int timer = 60;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mail_verification);
        ButterKnife.bind(this);
    }


    private void getVerfCode() {
        btnCode.setClickable(false);
        btnCode.post(new Runnable() {
            @Override
            public void run() {
                if (timer > 0) {
                    timer--;
                    btnCode.postDelayed(this, 1000);
                    btnCode.setText(timer + "s");
                } else {
                    timer = 60;
                    btnCode.setClickable(true);
                    btnCode.setText(getResources().getString(R.string.get_phone_code));
                }
            }
        });
        Map<String, Object> postParams = new HashMap<String, Object>();
        postParams.put("token", I_Share.readAccountToken());
        postParams.put("uid", I_Share.readAccountUserid());
        postParams.put("email", etMail.getText().toString());


        Functions.post(this, URLs.GET_MAIL_VERIFYCODE, postParams, new IHttpPostListener() {
            @Override
            public void onTimeOut() {

            }
            @Override
            public void responseSuccess(String result) {
                try {
                    if (Ipost.isSuccess(result)) {
                        IToast.show(getString(R.string.hint_mail_code_send_success));
                    } else {
                        IToast.show(Ipost.getErrorMsg(result));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, false, true);
    }


    private void verfyMail() {

        Map<String, Object> postParams = new HashMap<>();
        postParams.put("token", I_Share.readAccountToken());
        postParams.put("uid", I_Share.readAccountUserid());
        postParams.put("email", etMail.getText().toString());
        postParams.put("code", etCode.getText().toString());

        Functions.post(this, URLs.VERIFY_MAIL, postParams, new IHttpPostListener() {
            @Override
            public void onTimeOut() {

            }
            @Override
            public void responseSuccess(String result) {
                if (!TextUtils.isEmpty(result)) {

                    try {
                        JSONObject jo = new JSONObject(result);
                        String code = jo.getString("error");
                        if ("0".equalsIgnoreCase(code)) {
                            IToast.show(getString(R.string.hint_verify_mail_succeed));
                            setResult(CODE_REFRESH);
                            finish();
                        } else {
                            IToast.show(getString(R.string.hint_verify_mail_failed));
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, true, true);
    }


    @OnClick(R.id.btn_code)
    public void onBtnCodeClicked() {
        if (checkMail())
            getVerfCode();
    }

    @OnClick(R.id.btn_submit)
    public void onBtnSubmitClicked() {
        if (checkInfo())
            verfyMail();

    }


    private boolean checkMail() {
        boolean isValid = etMail.getText() != null && !TextUtils.isEmpty(etMail.getText().toString());
        boolean isEmail = false;

        if (isValid) {
            isEmail = RegUtils.isEmail(etMail.getText().toString());
        }

        boolean result = isValid && isEmail;
        if (!result) IToast.show(getString(R.string.plz_input_correct_mail));

        //正则check
        return result;

    }

    private boolean checkInfo() {
        boolean emailAvailable = checkMail();
        if (!emailAvailable) {
            return false;
        }
        boolean codeAvailable = !TextUtils.isEmpty(etCode.getText());
        boolean result = emailAvailable && codeAvailable;
        if (!result) IToast.show(getString(R.string.plz_input_mail_verification_code));
        return result;
    }




    //返回
    @OnClick(R.id.actionbar_back)
    void onClick_Back() {
        finish();
    }
}
